package com.ycsj.goldmedalnewconcept.view;

import java.util.ArrayList;
import java.util.List;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class FerrisWheelLayout extends RelativeLayout {
	
	private float mCurPosX;
	private float mCurPosY;
	private float mPosX;
	private float mPosY;
	private float countsX;
	private float countsY;

	public FerrisWheelLayout(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	public FerrisWheelLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public FerrisWheelLayout(Context context) {
		this(context, null);
	}

	Paint mPaint;
	Point childCenter = new Point();

	public int unitCount = 4;
	public int firstNum = 1;
	public int firstLayout = 1;

	public void setcount(int count) {
		unitCount = count;
	}

	public void setFirstNum(int count) {
		firstNum = count;
	}

	double firstChildDegee = 0.43;

	Point center = new Point();
	int radius;
	double cellDegree;

	private GestureDetector gestureDetector;
	private float x_tmp1;
	private float y_tmp1;
	private float x_tmp2;
	private float y_tmp2;

	private void init() {
		// 1 构造GestureDetector对象
		gestureDetector = new GestureDetector(getContext(), gestureListener);

		// blackPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		// blackPaint.setColor(Color.BLACK);
		// // 让画笔只描边
		// blackPaint.setStyle(Style.STROKE);
		// blackPaint.setStrokeWidth(3);
		//
		// redPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		// redPaint.setColor(Color.RED);
		//
		// grayPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		// grayPaint.setColor(Color.GRAY);
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint.setColor(Color.WHITE);

	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// 拦截所有的触摸事件，不再向下传递，保证自己的onTouchEvent得到执行

		return true;
	}

	public boolean onTouchEvent(MotionEvent event) {

	
		// 2 分析触摸事件
		// if(gestureDetector.onTouchEvent(event)){
		// Log.e("onTouchEvent", "命中了");
		// }
		gestureDetector.onTouchEvent(event);

		switch (event.getAction()) {

		case MotionEvent.ACTION_DOWN:
//			mPosX = event.getX();
//			mPosY = event.getY();
			break;
		case MotionEvent.ACTION_MOVE:
//			mCurPosX = event.getX();
//			mCurPosY = event.getY();
//
//			countsX += Math.abs(mCurPosX - mPosX);
//			countsY += Math.abs(mCurPosY - mPosY);
//			mPosX = event.getX();
//			mPosY = event.getY();
//			Log.e("test", countsX + countsY + "总");
//			if (countsX + countsY > 2000) {
//				Log.e("test", "停");
//				
//				if (valueAnimator != null && valueAnimator.isRunning()) {
//					valueAnimator.cancel();
//				}
//				
//				return false;
//			}
			break;
		case MotionEvent.ACTION_UP:
			countsX = 0;
			countsY = 0;
			mPosX = 0;
			mPosY = 0;
			mCurPosY = 0;
			mCurPosX = 0;
			break;
		}

		// //获取当前坐标
		// float x = event.getX();
		// float y = event.getY();
		//
		// switch (event.getAction()){
		// case MotionEvent.ACTION_DOWN:
		// x_tmp1 = x;
		// y_tmp1 = y;
		// break;
		// case MotionEvent.ACTION_UP:
		// x_tmp2 = x;
		//
		// y_tmp2 = y;
		// Log.i("test","滑动参值 x1="+ x_tmp1 +"; x2=" + x_tmp2);
		// if(x_tmp1 != 0 && y_tmp1 != 0){
		// if(y_tmp1 - y_tmp2 > 8){
		//
		// }
		// if(y_tmp2 - y_tmp1 > 8){
		// Toast.makeText(getContext(), "向xia滑动", 0).show();
		// }
		// }
		// break;
		// }
		// // 返回true表示需要消费触摸事件，触摸事件才能源源不断得传入此方法
		// if(unitCount<9){
		//
		//
		// TextView tvLast= (TextView) getChildAt(unitCount-1);
		// TextView tvFirst= (TextView) getChildAt(0);
		// if(tvLast.getX()>0&&tvLast.getY()>center.y&&y_tmp1 - y_tmp2 > 8){
		// return false;
		// }
		// if(tvFirst.getX()>0&&tvFirst.getY()<center.y&&y_tmp2 - y_tmp1 > 8){
		// return false;
		// }
		// }
		return true;

	};

	public void setDegree(double degree) {
		// 改变第一个孩子的角度
		firstChildDegee = degree;
		// 会导致onLayout方法调用，其他孩子的角度都根据第一个孩子的角度变化了
		requestLayout();

	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);

		computeValues();

		int childCount = getChildCount();

		for (int i = 0; i < childCount; i++) {
			View child = getChildAt(i);
			double childDegree = i * cellDegree + firstChildDegee;
			childCenter.x = (int) (radius * Math.sin(childDegree)) + center.x;
			childCenter.y = (int) (-radius * Math.cos(childDegree)) + center.y;
			// 此方法是指child在父控件中的位置
			child.layout(childCenter.x - child.getWidth() / 2, childCenter.y
					- child.getHeight() / 2, childCenter.x + child.getWidth()
					/ 2, childCenter.y + child.getHeight() / 2);
			// if(unitCount<=i){
			// child.setVisibility(View.INVISIBLE);
			// }

		}

		TextView childAt0 = (TextView) getChildAt(0);
		TextView childAt1 = (TextView) getChildAt(1);
		TextView childAt2 = (TextView) getChildAt(2);
		TextView childAt3 = (TextView) getChildAt(3);
		TextView childAt4 = (TextView) getChildAt(4);
		TextView childAt5 = (TextView) getChildAt(5);
		TextView childAt6 = (TextView) getChildAt(6);
		TextView childAt7 = (TextView) getChildAt(7);
		TextView childAt8 = (TextView) getChildAt(8);
		if (firstLayout == 1) {
			childAt0.setText(String.valueOf(firstNum));

			childAt1.setText(String.valueOf(firstNum + 1));
			childAt2.setText(String.valueOf(firstNum + 2));
			childAt3.setText(String.valueOf(firstNum + 3));
			childAt4.setText(String.valueOf(firstNum + 4));
			childAt5.setText(String.valueOf(firstNum + 5));
			childAt6.setText(String.valueOf(firstNum + 6));
			childAt7.setText(String.valueOf(firstNum + 7));
			childAt8.setText(String.valueOf(firstNum + 8));
		}
	}

	private void computeValues() {
		// 圆心
		// center.x = getWidth() / 2;
		// center.y = getHeight() / 2;
		center.x = 0;
		center.y = getHeight() / 2;
		// 半径
		// 先求出最高的孩子的高度和最宽的孩子的宽度
		int childCount = getChildCount();
		int maxChildHeight = 0;
		int maxChildWidth = 0;

		for (int i = 0; i < childCount; i++) {
			View child = getChildAt(i);
			if (child.getHeight() > maxChildHeight) {
				maxChildHeight = child.getHeight();
			}
			if (child.getWidth() > maxChildWidth) {
				maxChildWidth = child.getWidth();
			}
		}

		radius = Math.min(getWidth() / 3 * 2 - maxChildWidth / 2, getHeight()
				/ 2 - maxChildHeight / 3 * 2) + 100;
		// 间隔角度
		cellDegree = Math.PI * 2 / childCount;
	}

	@SuppressLint("WrongCall")
	@Override
	protected void dispatchDraw(Canvas canvas) {
		// 绘制背景，让孩子绘制自己
		super.dispatchDraw(canvas);
		// onDraw(canvas);
	}

	// ondraw表示的是绘制自身的，对于viewgroup来说，是不会调用此方法的
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// Log.e("onDraw", "onDraw");
		// 绘制圆心
		canvas.drawCircle(center.x, center.y, 4, mPaint);
		// 绘制圆
		canvas.drawCircle(center.x, center.y, radius, mPaint);
		// 绘制圆心与子控件中心点的连线
		// int childCount = getChildCount();
		// for (int i = 0; i < childCount; i++) {
		// double childDegree = i * cellDegree + firstChildDegee;
		// childCenter.x = (int) (radius * Math.sin(childDegree)) + center.x;
		// childCenter.y = (int) (-radius * Math.cos(childDegree)) + center.y;
		// canvas.drawLine(center.x, center.y, childCenter.x, childCenter.y,
		// mPaint);
		//
		// }
	}

	private float firstX;
	private float firstX1;
	private int[] arrs;
	List<TextView> leftViews = new ArrayList<TextView>();
	private OnGestureListener gestureListener = new OnGestureListener() {
		MotionEvent onDownMotionEvent;

		// 相当于 Action_down
		// 返回值（下同），true代表命中了手势，那么分析的onTouchEvent方法就会返回true
		@Override
		public boolean onDown(MotionEvent e) {
			
			mPosX = e.getX();
			mPosY = e.getY();
			// Log.d("onDown", " ");
			// Log.d("test", "1   " + getChildAt(0).getX()
			// + getChildAt(0).getTag());
			onDownMotionEvent = e;
			leftViews.removeAll(leftViews);
			for (int i = 0; i < 9; i++) {

				firstX = getChildAt(i).getX();
				if (firstX < 0) {
					leftViews.add((TextView) getChildAt(i));
				}
			}
			firstX1 = getChildAt(1).getX();

			TextView childAt0 = (TextView) getChildAt(0);
			TextView childAt1 = (TextView) getChildAt(1);
			TextView childAt2 = (TextView) getChildAt(2);
			TextView childAt3 = (TextView) getChildAt(3);
			TextView childAt4 = (TextView) getChildAt(4);
			TextView childAt5 = (TextView) getChildAt(5);
			TextView childAt6 = (TextView) getChildAt(6);
			TextView childAt7 = (TextView) getChildAt(7);
			TextView childAt8 = (TextView) getChildAt(8);
			int x = Integer.parseInt((String) childAt0.getText().toString());
			int x1 = Integer.parseInt((String) childAt1.getText().toString());
			int x2 = Integer.parseInt((String) childAt2.getText().toString());
			int x3 = Integer.parseInt((String) childAt3.getText().toString());
			int x4 = Integer.parseInt((String) childAt4.getText().toString());
			int x5 = Integer.parseInt((String) childAt5.getText().toString());
			int x6 = Integer.parseInt((String) childAt6.getText().toString());
			int x7 = Integer.parseInt((String) childAt7.getText().toString());
			int x8 = Integer.parseInt((String) childAt8.getText().toString());

			arrs = new int[] { x, x1, x2, x3, x4, x5, x6, x7, x8 };

			// 让值动画停下来
			if (valueAnimator != null && valueAnimator.isRunning()) {
				valueAnimator.cancel();
			}
			return false;
		}

		// 等同于 Action_Move
		// e1 这一组触摸事件的开始：down事件
		// e2 触发了onScroll调用的move事件，最新的一次的move事件
		// distanceX 上次触摸事件和本次触摸事件的距离差，在x轴
		// distanceY 上次触摸事件和本次触摸事件的距离差，在y轴
		// newMotionEvent - oldMotionEvent = delta
		// oldMotionEvent - newMotionEvent = distance
		// 想要知道上一次触摸事件的位置, oldMotionEvent = newMotionEvent + distance
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			
			
			mCurPosX = e2.getX();
			mCurPosY = e2.getY();

			countsX += Math.abs(mCurPosX - mPosX);
			countsY += Math.abs(mCurPosY - mPosY);
			mPosX = e2.getX();
			mPosY = e2.getY();
//			Log.e("test", countsX + countsY + "总");
			if (countsX + countsY > 700) {
				return false;
			}
			
			
			
			
			
			
			
			// 根据手势拖拽控件的相位而移动控件
			// setTranslationX(getTranslationX()+e2.getX() - e1.getX());
			// setTranslationY(getTranslationX()+e2.getY() - e1.getY());
			// return super.onScroll(e1, e2, distanceX, distanceY);
			// 新点对应的角度
			double newDegree = getPointDegree(e2.getX(), e2.getY());
			// 旧点对应的角度
			// distanceY=distanceY>20?20:distanceY;
			// distanceX=distanceX>20?20:distanceX;
			double oldDegree = getPointDegree(e2.getX() + distanceX, e2.getY()
					+ distanceY);
			// 角度差

			// double deltaDegree = newDegree - oldDegree>0.2?0.2:(newDegree -
			// oldDegree);
			double deltaDegree = newDegree - oldDegree;
			// 转动角度差那么多
			// setDegree(firstChildDegee + deltaDegree);
			// Log.d("onScroll", " ");
			TextView tvLast = (TextView) getChildAt(unitCount - 1);
			TextView tvFirst = (TextView) getChildAt(0);
			// if(tvLast.getX()>0&&newDegree < oldDegree){
			// return false;
			// }
			// if(tvFirst.getX()>0&&newDegree > oldDegree){
			// return false;
			// }
			WindowManager manager = (WindowManager) getContext()
					.getSystemService(Context.WINDOW_SERVICE);
			DisplayMetrics dm = new DisplayMetrics();
			manager.getDefaultDisplay().getMetrics(dm);
			int pmwidth = dm.widthPixels;
			int pmheight = dm.heightPixels;

			if (distanceX > (pmwidth / 5)) {
				// Toast.makeText(getContext(), "e1", 0).show();
				return false;
			}
			if (unitCount == 0) {
				unitCount = 18;
			}

			if (unitCount < 9) {
				// TextView tvLast= (TextView) getChildAt(unitCount-1);
				// TextView tvFirst= (TextView) getChildAt(0);
				if (null != tvLast) {

					String tvlastNum = tvLast.getText().toString();
					if (Integer.parseInt(tvlastNum) == unitCount
							&& tvLast.getX() > 0 && e2.getY() - e1.getY() < 0) {
						// Toast.makeText(getContext(), "滑动不了了", 0).show();
						return false;
					}

					if (tvLast.getX() > 0 && tvLast.getY() > center.y
							&& e1.getY() - e2.getY() > 8) {
						setDegree(firstChildDegee);
						return false;
					}
					if (tvFirst.getX() > 0 && tvFirst.getY() < center.y
							&& e2.getY() - e1.getY() > 8) {
						setDegree(firstChildDegee);
						return false;
					}
				}
			}

			setDegree(firstChildDegee + deltaDegree);

			setChildViewText(newDegree, oldDegree);

			// Log.e("test", ""+"=="+newDegree);
			return false;
		}

		private void setChildViewText(double newDegree, double oldDegree) {

			if (newDegree < oldDegree) {
				// Log.e("test", "up");

				TextView childAt0 = (TextView) getChildAt(0);
				TextView childAt1 = (TextView) getChildAt(1);
				TextView childAt2 = (TextView) getChildAt(2);
				TextView childAt3 = (TextView) getChildAt(3);
				TextView childAt4 = (TextView) getChildAt(4);
				TextView childAt5 = (TextView) getChildAt(5);
				TextView childAt6 = (TextView) getChildAt(6);
				TextView childAt7 = (TextView) getChildAt(7);
				TextView childAt8 = (TextView) getChildAt(8);

				// &&childAt0.getX()<=(center.x-childAt0.getWidth())

				if (unitCount > 9) {

					// if(childAt0.getX()<0&&"9".equals(childAt8.getText())&&childAt0.getX()<=(center.x-childAt0.getWidth())){
					// childAt0.setText("10");
					// }else
					// if(childAt0.getX()<0&&"18".equals(childAt8.getText())&&childAt0.getX()<=(center.x-childAt0.getWidth())){
					// childAt0.setText("1");
					// }
					if (childAt0.getX() < 0
							&& (firstNum + 8) == Integer.parseInt(childAt8
									.getText().toString())
							&& childAt0.getX() <= (center.x - childAt0
									.getWidth())) {
						childAt0.setText(String.valueOf(firstNum + 9));
						firstLayout = 2;
					} else if (childAt0.getX() < 0
							&& (firstNum + unitCount - 1) == Integer
									.parseInt(childAt8.getText().toString())
							&& childAt0.getX() <= (center.x - childAt0
									.getWidth())) {
						childAt0.setText(String.valueOf(firstNum));
					}

					if (childAt1.getX() < 0
							&& (firstNum + 9) == Integer.parseInt(childAt0
									.getText().toString())
							&& childAt1.getX() <= (center.x - childAt1
									.getWidth())) {
						childAt1.setText(String.valueOf(firstNum + 10));
					} else if (childAt1.getX() < 0
							&& (firstNum) == Integer.parseInt(childAt0
									.getText().toString())
							&& childAt1.getX() <= (center.x - childAt1
									.getWidth())) {
						childAt1.setText(String.valueOf(firstNum + 1));
					}

					if (childAt2.getX() < 0
							&& (firstNum + 10) == Integer.parseInt(childAt1
									.getText().toString())
							&& childAt2.getX() <= (center.x - childAt2
									.getWidth())) {
						childAt2.setText(String.valueOf(firstNum + 11));
					} else if (childAt2.getX() < 0
							&& (firstNum + 1) == Integer.parseInt(childAt1
									.getText().toString())
							&& childAt2.getX() <= (center.x - childAt2
									.getWidth())) {
						childAt2.setText(String.valueOf(firstNum + 2));
					}

					if (childAt3.getX() < 0
							&& (firstNum + 11) == Integer.parseInt(childAt2
									.getText().toString())
							&& childAt3.getX() <= (center.x - childAt3
									.getWidth())) {
						childAt3.setText(String.valueOf(firstNum + 12));
					} else if (childAt3.getX() < 0
							&& (firstNum + 2) == Integer.parseInt(childAt2
									.getText().toString())
							&& childAt3.getX() <= (center.x - childAt3
									.getWidth())) {
						childAt3.setText(String.valueOf(firstNum + 3));
					}

					if (childAt4.getX() < 0
							&& (firstNum + 12) == Integer.parseInt(childAt3
									.getText().toString())
							&& childAt4.getX() <= (center.x - childAt4
									.getWidth())) {
						childAt4.setText(String.valueOf(firstNum + 13));
					} else if (childAt4.getX() < 0
							&& (firstNum + 3) == Integer.parseInt(childAt3
									.getText().toString())
							&& childAt4.getX() <= (center.x - childAt4
									.getWidth())) {
						childAt4.setText(String.valueOf(firstNum + 4));
					}

					if (childAt5.getX() < 0
							&& (firstNum + 13) == Integer.parseInt(childAt4
									.getText().toString())
							&& childAt5.getX() <= (center.x - childAt5
									.getWidth())) {
						childAt5.setText(String.valueOf(firstNum + 14));
					} else if (childAt5.getX() < 0
							&& (firstNum + 4) == Integer.parseInt(childAt4
									.getText().toString())
							&& childAt5.getX() <= (center.x - childAt5
									.getWidth())) {
						childAt5.setText(String.valueOf(firstNum + 5));
					}

					if (childAt6.getX() < 0
							&& (firstNum + 14) == Integer.parseInt(childAt5
									.getText().toString())
							&& childAt6.getX() <= (center.x - childAt6
									.getWidth())) {
						childAt6.setText(String.valueOf(firstNum + 15));
					} else if (childAt6.getX() < 0
							&& (firstNum + 5) == Integer.parseInt(childAt5
									.getText().toString())
							&& childAt6.getX() <= (center.x - childAt6
									.getWidth())) {
						childAt6.setText(String.valueOf(firstNum + 6));
					}

					if (childAt7.getX() < 0
							&& (firstNum + 15) == Integer.parseInt(childAt6
									.getText().toString())
							&& childAt7.getX() <= (center.x - childAt7
									.getWidth())) {
						childAt7.setText(String.valueOf(firstNum + 16));
					} else if (childAt7.getX() < 0
							&& (firstNum + 6) == Integer.parseInt(childAt6
									.getText().toString())
							&& childAt7.getX() <= (center.x - childAt7
									.getWidth())) {
						childAt7.setText(String.valueOf(firstNum + 7));
					}

					if (childAt8.getX() < 0
							&& (firstNum + 16) == Integer.parseInt(childAt7
									.getText().toString())
							&& childAt8.getX() <= (center.x - childAt8
									.getWidth())) {
						childAt8.setText(String.valueOf(firstNum + 17));
					} else if (childAt8.getX() < 0
							&& (firstNum + 7) == Integer.parseInt(childAt7
									.getText().toString())
							&& childAt8.getX() <= (center.x - childAt8
									.getWidth())) {
						childAt8.setText(String.valueOf(firstNum + 8));
					}
					Log.e("test", "down" + childAt8.getText().toString());
				}
			} else {
				// Log.e("test", "down");
				TextView childAt0 = (TextView) getChildAt(0);
				TextView childAt1 = (TextView) getChildAt(1);
				TextView childAt2 = (TextView) getChildAt(2);
				TextView childAt3 = (TextView) getChildAt(3);
				TextView childAt4 = (TextView) getChildAt(4);
				TextView childAt5 = (TextView) getChildAt(5);
				TextView childAt6 = (TextView) getChildAt(6);
				TextView childAt7 = (TextView) getChildAt(7);
				TextView childAt8 = (TextView) getChildAt(8);
				if (unitCount > 9) {

					// if(childAt0.getX()<0&&"2".equals(childAt1.getText())&&childAt0.getX()<=(center.x-childAt0.getWidth())){
					// childAt0.setText("1");
					// }else
					// if(childAt0.getX()<0&&"11".equals(childAt1.getText())&&childAt0.getX()<=(center.x-childAt0.getWidth())){
					// childAt0.setText("10");
					// }
					if (childAt0.getX() < 0
							&& (firstNum + 1) == Integer.parseInt(childAt1
									.getText().toString())
							&& childAt0.getX() <= (center.x - childAt0
									.getWidth())) {
						childAt0.setText(String.valueOf(firstNum));
					} else if (childAt0.getX() < 0
							&& (firstNum + 10) == Integer.parseInt(childAt1
									.getText().toString())
							&& childAt0.getX() <= (center.x - childAt0
									.getWidth())) {
						childAt0.setText(String.valueOf(firstNum + 9));
					}

					if (childAt1.getX() < 0
							&& (firstNum + 2) == Integer.parseInt(childAt2
									.getText().toString())
							&& childAt1.getX() <= (center.x - childAt1
									.getWidth())) {
						childAt1.setText(String.valueOf(firstNum + 1));
					} else if (childAt1.getX() < 0
							&& (firstNum + 11) == Integer.parseInt(childAt2
									.getText().toString())
							&& childAt1.getX() <= (center.x - childAt1
									.getWidth())) {
						childAt1.setText(String.valueOf(firstNum + 10));
					}

					if (childAt2.getX() < 0
							&& (firstNum + 3) == Integer.parseInt(childAt3
									.getText().toString())
							&& childAt2.getX() <= (center.x - childAt2
									.getWidth())) {
						childAt2.setText(String.valueOf(firstNum + 2));
					} else if (childAt2.getX() < 0
							&& (firstNum + 12) == Integer.parseInt(childAt3
									.getText().toString())
							&& childAt2.getX() <= (center.x - childAt2
									.getWidth())) {
						childAt2.setText(String.valueOf(firstNum + 11));
					}

					if (childAt3.getX() < 0
							&& (firstNum + 4) == Integer.parseInt(childAt4
									.getText().toString())
							&& childAt3.getX() <= (center.x - childAt3
									.getWidth())) {
						childAt3.setText(String.valueOf(firstNum + 3));
					} else if (childAt3.getX() < 0
							&& (firstNum + 13) == Integer.parseInt(childAt4
									.getText().toString())
							&& childAt3.getX() <= (center.x - childAt3
									.getWidth())) {
						childAt3.setText(String.valueOf(firstNum + 12));
					}

					if (childAt4.getX() < 0
							&& (firstNum + 5) == Integer.parseInt(childAt5
									.getText().toString())
							&& childAt4.getX() <= (center.x - childAt4
									.getWidth())) {
						childAt4.setText(String.valueOf(firstNum + 4));
					} else if (childAt4.getX() < 0
							&& (firstNum + 14) == Integer.parseInt(childAt5
									.getText().toString())
							&& childAt4.getX() <= (center.x - childAt4
									.getWidth())) {
						childAt4.setText(String.valueOf(firstNum + 13));
					}

					if (childAt5.getX() < 0
							&& (firstNum + 6) == Integer.parseInt(childAt6
									.getText().toString())
							&& childAt5.getX() <= (center.x - childAt5
									.getWidth())) {
						childAt5.setText(String.valueOf(firstNum + 5));
						firstLayout = 2;
					} else if (childAt5.getX() < 0
							&& (firstNum + 15) == Integer.parseInt(childAt6
									.getText().toString())
							&& childAt5.getX() <= (center.x - childAt5
									.getWidth())) {
						firstLayout = 2;
						childAt5.setText(String.valueOf(firstNum + 14));
					}

					if (childAt6.getX() < 0
							&& (firstNum + 7) == Integer.parseInt(childAt7
									.getText().toString())
							&& childAt6.getX() <= (center.x - childAt6
									.getWidth())) {
						childAt6.setText(String.valueOf(firstNum + 6));
					} else if (childAt6.getX() < 0
							&& (firstNum + 16) == Integer.parseInt(childAt7
									.getText().toString())
							&& childAt6.getX() <= (center.x - childAt6
									.getWidth())) {
						childAt6.setText(String.valueOf(firstNum + 15));
					}

					if (childAt7.getX() < 0
							&& (firstNum + 8) == Integer.parseInt(childAt8
									.getText().toString())
							&& childAt7.getX() <= (center.x - childAt7
									.getWidth())) {
						childAt7.setText(String.valueOf(firstNum + 7));
					} else if (childAt7.getX() < 0
							&& (firstNum + 17) == Integer.parseInt(childAt8
									.getText().toString())
							&& childAt7.getX() <= (center.x - childAt7
									.getWidth())) {
						childAt7.setText(String.valueOf(firstNum + 16));
					}

					if (childAt8.getX() < 0
							&& (firstNum + 9) == Integer.parseInt(childAt0
									.getText().toString())
							&& childAt8.getX() <= (center.x - childAt8
									.getWidth())) {
						childAt8.setText(String.valueOf(firstNum + 8));
						firstLayout = 2;
					} else if (childAt8.getX() < 0
							&& (firstNum) == Integer.parseInt(childAt0
									.getText().toString())
							&& childAt8.getX() <= (center.x - childAt8
									.getWidth())) {
						childAt8.setText(String.valueOf(firstNum + 17));
					}
				}
			}
		}

		// 当手指离开屏幕时，还具有一定的速度，会调用此方法
		// e1 按下事件
		// e2 最后一次的move事件
		// velocityX 松手时，滑动的速度，x轴，单位是像素/s
		// velocityY 松手时，滑动的速度，y轴，单位是像素/s
		// 松手时的转动的角速度 （把 velocityX 和 velocityY --> velDegree）
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			// Log.d("onFling", " ");
			//
			// // 得到1ms后的角度
			// double after1MsDegree = getPointDegree(
			// e2.getX() + velocityX / 1000, e2.getY() + velocityY / 1000);
			// // 得到当前角度
			// double currentDegree = getPointDegree(e2.getX(), e2.getY());
			// // 得到1ms后的角度变化量
			// double degreeDelta1Ms = after1MsDegree - currentDegree;
			// // 得到1s后的角度变化量，就是角速度了
			// double velDegree = degreeDelta1Ms * 1000;
			// // 通过角速度让控件转动
			// rollInAnimation(velDegree / 400);
			//
			// if ((getChildAt(0).getX() + (getChildAt(0).getWidth())) != 0) {
			// // Log.e("test", ""+getChildAt(0).getTag());
			// Log.e("test", "" + getPointDegree(childCenter.x, childCenter.y)
			// * 180 / Math.PI);
			// }
			//
			return false;
		}

		// 相当于点击
		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			// Log.d("onSingleTapUp", " ");
			// 单击了，
			// 根据触摸事件的位置，找到孩子的角标
			int index = findTouchChildIndex(e.getX(), e.getY());
			if (index >= 0) {
				getChildAt(index).performClick();
			}
			// 触摸孩子的点击事件
			return false;
		}

		// 当按下后，一小会后还没有移动，会调用此方法
		@Override
		public void onShowPress(MotionEvent e) {
			// Log.d("onShowPress", " ");

		}

		// 长按
		@Override
		public void onLongPress(MotionEvent e) {
			// Log.d("onLongPress", " ");

		}

	};
	private ValueAnimator valueAnimator;
	private float lastX;
	private float lastX1;

	private void rollInAnimation(double velDegree) {

		// 利用传入的角速度，计算出动画的事件，结束值，
		// 动画的起始值，就是当前的角度，不用求
		long duration = (long) (Math.abs(velDegree) / Math.PI * 1000);
		double degreeDeltaTotal = velDegree * duration / 1000 / 2;
		valueAnimator = ValueAnimator.ofFloat((float) firstChildDegee,
				(float) (firstChildDegee + degreeDeltaTotal));
		// 值动画最重要的是：可以设置一个监听，得到在duration中的值的变化
		valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				// Log.d("onAnimationUpdate", "" +
				// (Float)animation.getAnimatedValue());
				setDegree((Float) animation.getAnimatedValue());

			}
		});
		// 如果对动画使用了减速插值器，就可以获得从快逐渐变慢的效果了
		valueAnimator.setInterpolator(new DecelerateInterpolator(2));
		valueAnimator.setDuration(duration);
		valueAnimator.start();

		// Log.d("test", " " + getChildAt(0).getX());

		lastX = getChildAt(0).getX();
		lastX1 = getChildAt(1).getX();
	}

	protected int findTouchChildIndex(float x, float y) {
		int childCount = getChildCount();
		for (int i = 0; i < childCount; i++) {
			View child = getChildAt(i);
			if (x > child.getLeft() && x < child.getRight()
					&& y > child.getTop() && y < child.getBottom()) {
				return i;
			}
		}

		return -1;
	}

	private double getPointDegree(float x, float y) {
		return Math.atan2(y - center.y, x - center.x);
	}
}
